import Utils from "../../../Utils";
import DefensiveFacility from "../../facility/DefensiveFacility";
import HealthPoint from "../../funcObj/interface/HealthPoint";
import { Global } from "../../info/Global";
import GameManager from "../../manager/GameManager";
import DefenseFlyer from "../DefenseFlyer";

const { ccclass, property } = cc._decorator;

/**
 * 蜂刺反导导弹
 */
@ccclass
export default class MediumRangeAntimissileMissile extends DefenseFlyer {

    @property(cc.Animation)
    tail: cc.Animation = null;

    focus: boolean;

    public init(base: DefensiveFacility) {
        super.init(base)
        this.tail.stop();
        this.tail.getComponent(cc.Sprite).spriteFrame = null;
        this.atk = this.flyerInfo.攻击力;
        this.moveComp.clamp = false;
        this.focus = false;
    }

    public dispatch(target: HealthPoint) {
        super.dispatch(target);
        this.target = target;
        this.tail.play();

        let targetAngle = this.getTargetAngel();
        if (targetAngle > 180) this.node.angle = 360;
        this.moveComp.moveSpeed = this.flyerInfo.移动速度;
        this.update(0);

    }

    private getTargetPosition(): cc.Vec3 {
        return this.target ? this.target.node.position : this.targetPosition;
    }

    private getTargetAngel() {
        let thisPosition = this.node.position;
        let targetPosition = this.getTargetPosition();
        let direction = cc.v2(targetPosition.x - thisPosition.x, targetPosition.y - thisPosition.y);
        let targetAngle = cc.misc.radiansToDegrees(cc.Vec2.angle(direction, cc.v2(0, 1)));
        targetAngle = direction.x > 0 ? 360 - targetAngle : targetAngle;
        return targetAngle;
    }

    onTargetDie(): void {
        super.onTargetDie();
        this.onArrive();
    }

    protected update(dt): void {
        if (!this.target && !this.targetPosition) return;
        let turnSpeed = 300;
        // 对准怪物
        let targetAngle = this.getTargetAngel();

        if (targetAngle - this.node.angle > 180) {
            this.node.angle += 360;
        }
        if (this.node.angle - targetAngle > 180) {
            this.node.angle -= 360;
        }

        //拖尾为中心 旋转
        //弹头判断是否抵达
        if (this.focus) {
            this.node.angle = targetAngle;
            this.moveComp.setDirection(Utils.angleToVectorCustom(this.node.angle));
        } else {
            let angleStep = turnSpeed * dt;

            let dis = Math.abs(targetAngle - this.node.angle);
            let dir = targetAngle >= this.node.angle ? 1 : -1
            let next: number = this.node.angle + angleStep * dir;
            if (Math.abs(dis) > angleStep) {
                this.node.angle = next;
            } else {
                this.node.angle = targetAngle;
                this.focus = true;
            }
            this.moveComp.setDirection(Utils.angleToVectorCustom(this.node.angle));
        }
        let targetPosition = this.getTargetPosition();
        let seltPosition = this.node.position;
        let distanse = targetPosition.sub(seltPosition).mag();
        if (distanse < this.node.height / 2) {
            this.onArrive();
        }
    }

    addBulletEffect(): void {
        let size = this.node.getContentSize();
        size.height *= 1.5;
        size.width = size.height;
        GameManager.instance.addFlyerExploadEffect(this.flyerType, this.node.position, size);
    }
}
